package com.example.ytyproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ytyproject.config.SecurityUtil;
import com.example.ytyproject.config.exception.AppException;
import com.example.ytyproject.entity.ShopMember;
import com.example.ytyproject.entity.ShopsBase;
import com.example.ytyproject.entity.vo.R;
import com.example.ytyproject.mapper.ShopMemberMapper;
import com.example.ytyproject.mapper.ShopsBaseMapper;
import com.example.ytyproject.service.ShopMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 商铺会员管理 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-13
 */
@Service
public class ShopMemberServiceImpl extends ServiceImpl<ShopMemberMapper, ShopMember> implements ShopMemberService {

    @Autowired
    private ShopsBaseMapper shopsBaseMapper;

    @Override
    public R listByPage(ShopMember param) {
        // 根据商铺id和手机号判断会员是否存在
        QueryWrapper<ShopMember> queryWrapper = new QueryWrapper();
        LambdaQueryWrapper<ShopMember> lambda = queryWrapper.lambda();
        String telephone = param.getTelephone();
        if (StringUtils.isNotEmpty(telephone)) {
            lambda.likeLeft(ShopMember::getTelephone, telephone);
        }
        Page<ShopMember> page = this.page(new Page<>(param.getPageNo(), param.getPageSize()), queryWrapper);
        long total = page.getTotal();
        if (total == 0) {
            return R.ok(page);
        }
        List<ShopMember> records = page.getRecords();
        List<String> shopIds = records.stream().map(ShopMember::getShopId).collect(Collectors.toList());
        List<ShopsBase> shopsBases = shopsBaseMapper.selectBatchIds(shopIds);
        for (ShopMember member : records) {
            Optional<ShopsBase> first = shopsBases.stream().filter(e -> e.getId().equals(member.getShopId())).findFirst();
            first.ifPresent(e -> member.setShopName(e.getShopName()));
        }
        return R.ok(page);
    }

    @Override
    public void addData(ShopMember param) {
        // 根据商铺id和手机号判断会员是否存在
        QueryWrapper<ShopMember> queryWrapper = new QueryWrapper();
        LambdaQueryWrapper<ShopMember> lambda = queryWrapper.lambda();
        lambda.eq(ShopMember::getShopId, param.getShopId()).eq(ShopMember::getTelephone, param.getTelephone());
        ShopMember member = this.getOne(queryWrapper);
        if (member != null) {
            throw new AppException("会员已经存在！");
        }
        param.setCreateDate(LocalDateTime.now()).setCreateId(SecurityUtil.getUserId())
                .setUpdateDate(LocalDateTime.now()).setUpdateId(SecurityUtil.getUserId()).setDeleted(false);
        this.save(param);
    }

    @Override
    public void updateData(ShopMember param) {

    }
}
